/**
 * @Title: settleAnalyseMain_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author QIANDUO875
 * @date 2014年9月26日 上午10:32:01
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('SettleAnalyseRegionCtrl',
	    ['$scope','SettleAnalyseRegionService', '$stateParams','Modal','SettleAnalyseService','BaseURL','CommunityService',
	     function($scope,SettleAnalyseRegionService,$stateParams,Modal,SettleAnalyseService,baseURL,CommunityService){
		
	    	/*********************************************************
			 * 
			 * Scope & Variables Init
			 * 
			 */
	    	
	    	var checkedArray = [],
	    		currentLevelType = '', 
	    		basicTableData = [],
	    		categoryType = '';
	    	
	    	$scope.page = {
	    		limit:10
	    	};
	    	

			/*********************************************************
			 * 
			 * private function
			 * 
			 */
			function getOrgLevelIndex() {
				if(currentLevelType === 4) {
					return 1;
				}
				else if(currentLevelType === 3) {
					return 2;
				}
				else if(currentLevelType === 2) {
					return 3;
				}
				else if (categoryType){
					return 1;
				}
				return null;
			}
	    	
			//查询
	    	function query(params) {
	    		
	    		var orgId = $scope.$parent.query.org;
	    		params = $.extend({
					orgId:orgId,
					date:$scope.$parent.query.date,
					indicator:$stateParams.indicator,
					orgLevel:currentLevelType,
					orgCategory:categoryType,
					start:1,
					limit:$scope.page.limit
				},params);
				SettleAnalyseRegionService.listRegionOrgs(params).success(function(res){
					if (res.data) {
						var arr = basicTableData.concat([]);
						arr.splice.apply(arr, [getOrgLevelIndex(),0].concat(toFixedDatas(res.data)));
						SettleAnalyseService.checkedInit(arr);
						$scope.tableData = arr;
						$scope.page.total = res.total;
					}
					else {
						$scope.page.total = 0;
						$scope.tableData = basicTableData;
					}
				});
	    	}
	    	
	    	function queryOrgCount() {
	    		SettleAnalyseRegionService.queryOrgCount($scope.$parent.query).
	    			success(function(res){
	    				if (res) {
	    					var arr = res.concat([]);
		    				basicTableData = arr;
		    				SettleAnalyseService.checkedInit(arr);
		    				$scope.tableData  = toFixedDatas(arr);
	    				}
	    				else {
	    					$scope.tableData  = [];
	    				}
	    			});
	    	}
	    	
			/*********************************************************
			 * 
			 * Event Handler
			 * 
			 */
	    	//查询
			$scope.$on('settleAnalyse.searchQuery', function() {
				currentLevelType = '';
				categoryType = '';
				queryOrgCount();
				$scope.page.total = 0;
				$scope.$parent.$broadcast('settleAnalyse.rowCheckChange',SettleAnalyseService.checkedArray);
	    	});
	    	
			//重置
			$scope.$on('settleAnalyse.searchReset', function(){
	    		$scope.$parent.query.orgCategory = '';
	    		$scope.$parent.query.orgLevel = '';
	    		$scope.$parent.query.org = '';
	    		$scope.$parent.query.doctor = '';
	    		$scope.$parent.query.depart = '';
	    	});
			//是否直接赋值
			var isAssignment = true; 
			
			$scope.$on('settleAnalyse.dataTypeChange', function(e, type){
				$scope.page.current = 1;
				isAssignment = false;
			});
			
			$scope.$on('pageTool.skipPage',function(e,v){
				query({
					start:v
				});
			});
			
			// 导出
			$scope.onExport = function(){
				//判断日期跨度不能大于一年
				var checkTemp = CommunityService.checkDateSpan($scope.$parent.query.date.start,$scope.$parent.query.date.end);
				if(checkTemp!=''&& checkTemp.length>0){
					Modal.alert(checkTemp);
					return false;
				}
				var date= $scope.$parent.query.date;
				var startDate = date.start;
				var endDate = date.end;
				var indicator = $stateParams.indicator;
				var orgCategory = $scope.$parent.query.orgCategory || '';
				var url=baseURL+"indication/regionExport.do" +
						"?level="+currentLevelType+"&indicator="+indicator+"&dataType="+SettleAnalyseService.typeSelected+
						"&startDate="+startDate+"&endDate="+endDate+"&type="+categoryType+"&orgCategory="+orgCategory;
			    window.location.href = url;
			}
			
			//chebox 勾选
			$scope.onRowCheckChange = function(data) {
				checkedArray = SettleAnalyseService.checkedArray;
	    		if(data.checked && checkedArray.length >= 2) {
	    			Modal.alert('勾选超过2个!');
	    			data.checked = false;
	    			return;
	    		}
	    		var index = checkedArray.indexOf(data);
	    		if(data.checked && index == -1) {
	    			checkedArray.push(data);
	    		} else {
	    			if(index != -1) {
	    				checkedArray.splice(index,1);
	    			}
	    		}
	    		$scope.$parent.$broadcast('settleAnalyse.rowCheckChange',checkedArray);
			}
			
			//点击展开事件
			$scope.onOrgLevelRowClick = function(level,type) {
				if((!level 
				  && !type ) 
				  || ( level == currentLevelType
				  && categoryType == type) ) {
					return;
				}	
				if(SettleAnalyseService.checkedArray.length == 0){
					SettleAnalyseService.checkedArray = [];
				}
				$scope.page.current = 1;
				
				if (level) {
					currentLevelType = level;
				}
				else {
					currentLevelType = '';
				}
				
				if (type) {
					categoryType = type;
				}
				else {
					categoryType = '';
				}
				SettleAnalyseService.orgLevel = currentLevelType;
				query({
	    			start:1
	    		});
//				$scope.$parent.$broadcast('settleAnalyse.rowCheckChange',SettleAnalyseService.checkedArray);
			}
			
			//以万为单位
			function toFixedDatas(datas) {
				var newDatas = [];
				if ($scope.$parent
					&& $scope.$parent.query 
					&& $scope.$parent.query.indicator ) {
						var isFlag = false;
						angular.forEach($scope.$parent.query.indicators,function(indicator,index){
							if ($scope.$parent.query.indicator == indicator) {
								isFlag = true;
							}
						});
						newDatas = toNewDatas(datas,isFlag);
					}
				return newDatas;
			}
			
			function toNewDatas(datas,isFlag) {
				angular.forEach(datas,function(data,index){
					if (isFlag) {
						data.first = (parseFloat(data.first)/10000).toFixed(2);
						data.second = (parseFloat(data.second)/10000).toFixed(2);
						data.third = (parseFloat(data.third)/10000).toFixed(2);
						data.fourth = (parseFloat(data.fourth)/10000).toFixed(2);
						data.fifth = (parseFloat(data.fifth)/10000).toFixed(2);
						data.sixth = (parseFloat(data.sixth)/10000).toFixed(2);
						data.seventh = (parseFloat(data.seventh)/10000).toFixed(2);
						data.eighth = (parseFloat(data.eighth)/10000).toFixed(2);
						data.ninth = (parseFloat(data.ninth)/10000).toFixed(2);
						data.tenth = (parseFloat(data.tenth)/10000).toFixed(2);
						data.eleventh = (parseFloat(data.eleventh)/10000).toFixed(2);
						data.twelfth = (parseFloat(data.twelfth)/10000).toFixed(2);
					}
					else {
						data.first = parseFloat(data.first).toFixed(2);
						data.second = parseFloat(data.second).toFixed(2);
						data.third = parseFloat(data.third).toFixed(2);
						data.fourth = parseFloat(data.fourth).toFixed(2);
						data.fifth = parseFloat(data.fifth).toFixed(2);
						data.sixth = parseFloat(data.sixth).toFixed(2);
						data.seventh = parseFloat(data.seventh).toFixed(2);
						data.eighth = parseFloat(data.eighth).toFixed(2);
						data.ninth = parseFloat(data.ninth).toFixed(2);
						data.tenth = parseFloat(data.tenth).toFixed(2);
						data.eleventh = parseFloat(data.eleventh).toFixed(2);
						data.twelfth = parseFloat(data.twelfth).toFixed(2);
					}
					data.yoy = parseFloat(data.yoy).toFixed(2);
					data.mom = parseFloat(data.mom).toFixed(2);
				});
				return datas;
			}
			/*********************************************************
			 * 
			 * Init
			 * 
			 */
			$scope.settleAnalyseService = SettleAnalyseService;
			$scope.$watchCollection('settleAnalyseService.typeSelected',function(v){
				if (v ) {
					listBaiscTableData();
				}
	    	});
			
			//获取等级数据
			function listBaiscTableData() {
				SettleAnalyseRegionService.listBaiscTableData($scope.$parent.query).
	    		success(function(res){
		    		basicTableData = res || [];
		    		if ($scope.$parent.query.orgLevel) {
		    			var datas = [];
		    			angular.forEach(basicTableData,function(data,index){
		    				if ($scope.$parent.query.orgLevel == data.level) {
		    					datas.push(data);
		    				}
		    			});
		    			basicTableData = datas;
		    		}
		    		//是否有值
		    		if (basicTableData) {
		    			basicTableData = toFixedDatas(basicTableData);
		    		}
		    		
		    		//是否直接赋值
		    		if (isAssignment) {
		    			$scope.tableData  = basicTableData;
		    		}
		    		else {
		    			isAssignment = true;
		    			query({
							start:1
						});
		    		}
		    	});
			}
	}]);
	
})();